Ext.ux.AddEquipment = Ext.extend(Ext.Panel, {
	initComponent : function(config) {
		var basePath = document.getElementById('basePath').href;
		var lang;
		var category = new Ext.data.Store( {
			proxy: new Ext.data.HttpProxy({url: basePath + 'base/list.action?whoCategory=equipment'}),
			reader:new Ext.data.ArrayReader({idIndex: 0},
				Ext.data.Record.create([{name: 'id'}, {name: 'name'}]))
		});
		
		var langStore = new Ext.data.ArrayStore({
	        fields: ['lang', 'value'],
	        data : [['简体中文', 'zh_CN'],['繁体中文', 'zh_TW'],['美式英文', 'en_US']]
	    });
		
		var addEquipmentForm = new Ext.FormPanel({
			labelWidth : 100, // label settings here cascade unless overridden
			url : basePath + 'equipment/add.action',
			autoScroll:true,
			frame : true,
			fileUpload: true,
			bodyStyle : 'padding:5px 5px 0',
			width : '100%',
			defaults : {
				width : 220
			},
			defaultType : 'textfield',

			items : [ {
				xtype : "combo",
				fieldLabel : '选择语言',
				store : langStore,
				mode : 'local',
				valueField: 'value',
				displayField : 'lang',
				allowBlank : false,
				typeAhead : true,
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						category.removeAll();
						lang = record.get('value');
						category.proxy.setUrl(basePath + 'base/list.action?whoCategory=equipment&lang=' + lang);
						category.load();
					}
				}
			}, {
				fieldLabel : '设备名称',
				name : 'equipment.name',
				allowBlank : false
			}, {
				fieldLabel : '设备型号',
				name : 'equipment.model',
				allowBlank : false
			}, {
				fieldLabel : '设备数量',
				name : 'equipment.count',
				allowBlank : false
			}, {
				xtype : "combo",
				fieldLabel : '设备类别',
				store : category,
				displayField : 'name',
				typeAhead : true,
				valueField: 'id',
				name:'category',
				mode : 'local',
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true
			}, {
				xtype:'datefield',
				fieldLabel : '购买时间',
				name : 'equipment.buyTime',
				format:'Y-m-d h:m:s',
				allowBlank : false
			}, {
				xtype:'StarHtmleditor',
				fieldLabel : '设备简介',
				anchor:'95%',
				height:300,
				allowBlank : false,
				autoScroll:true,
				url:basePath + 'base/upload.action',
				name : 'equipment.notes'
			}, {
				fieldLabel : '设备图片',
				name:'equipmentImage',
				inputType : 'file',
				allowBlank : false,
				editable:false
			}],
			bbar : [{
				xtype:'spacer',
				width:50
			},{
				text:'添加图片',
				tooltip:'添加多张设备图片',
				tooltipType:'qtip',
				handler:function() {
					if(!addEquipmentForm.form.isValid()) {
						return;
					}
					addEquipmentForm.add({
						fieldLabel : '设备图片',
						name:'equipmentImage',
						inputType : 'file',
						allowBlank : true,
						editable:false
					});
					addEquipmentForm.doLayout();
				}
			}, '->', {
				text : '保存',
				handler:function() {
					addEquipmentForm.form.submit( {
						waitMsg : '正在上传......',
						url : basePath + 'equipment/add.action?equipment.lang=' + lang,
						success : function(form, action) {
							form.reset();
							Ext.Msg.alert('提示', '添加设备成功');
						},
						failure : function(form, action) {
							form.reset();
							if (action.failureType == Ext.form.Action.SERVER_INVALID)
								Ext.MessageBox.alert('警告', action.result.msg);
						}
					});
				}
			}, '-', {
				text : '重置',
				handler:function() {
					addEquipmentForm.form.reset();
				}
			}, {
				xtype:'spacer',
				width:50
			}]
		})
		
		Ext.apply(this, {
			title : '添加设备',
			closable : 'true',
			autoScroll:true,
			frame : true,
			bodyStyle : 'padding:5px 5px 0',
			width : 370,
			items:[addEquipmentForm]
		});
		Ext.ux.AddEquipment.superclass.initComponent.apply(this, arguments);
	}
});
Ext.reg('lgnAddEquipment', Ext.ux.AddEquipment);
